//
//  SceneSetViewController.m
//  GBKHOME
//
//  Created by XMYY-21 on 2017/9/26.
//
//

#import "SceneSetViewController.h"
#import "SceneSetDetailedViewController.h"

#import "PresentTransitionAnimated.h"
#import "DismissTransitionAnimated.h"

#import "AppDelegate.h"
#import "GKBEquipmentsManager.h"

@interface SceneSetViewController ()<UIViewControllerTransitioningDelegate>
@property (retain, nonatomic) UIPercentDrivenInteractiveTransition *percentDrivenTransition;

@property (retain, nonatomic) IBOutlet UIButton *sceneBtn1;
@property (retain, nonatomic) IBOutlet UIButton *sceneBtn2;
@property (retain, nonatomic) IBOutlet UIButton *sceneBtn3;
@property (retain, nonatomic) IBOutlet UIButton *sceneBtn4;
@property (retain, nonatomic) IBOutlet UIButton *sceneBtn5;
@property (retain, nonatomic) IBOutlet UIButton *sceneBtn6;

@end

@implementation SceneSetViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self getSceneList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)sceneSetChange:(NSNotification *)noti{
    NSLog(@"getChange");
}

-(void)getSceneList{
    /*Scene structure
     Scene[
     SceneSetDic{"name":"sceneName",
     "sceneSetDevice":[ScenSetDeviceArr[deviceDic,deviceDic2]]},
     
     SceneSetDic2{"name":"sceneName",
     "sceneSetDevice":[ScenSetDeviceArr[deviceDic,deviceDic2]]}
     ]
     ===============
     deviceDic{
     "name":"deviceName",
     "switchId":"switchid"
     }
     */
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSArray *SceneArr =  [userDefaults arrayForKey:@"Scene"];
    if (SceneArr != nil) {
        
        [self setSceneBtnTitle:SceneArr];

    }else{
        NSMutableArray *tempScene = [NSMutableArray array];
        for (int i = 1; i < 7; i++) {
            NSMutableDictionary *tempSceneSetDic = [NSMutableDictionary dictionary];
            [tempSceneSetDic setValue:[NSString stringWithFormat:@"场景%d",i] forKey:@"name"];
            [tempSceneSetDic setObject:[NSArray array] forKey:@"sceneSetDevice"];
            [tempScene addObject:tempSceneSetDic];
        }
        
        [userDefaults setObject:tempScene forKey:@"Scene"];
        
        [self setSceneBtnTitle:tempScene];
        
        [userDefaults synchronize];
    }
}

-(void)setSceneBtnTitle:(NSArray *)array{
    NSDictionary *tempDic1 = array[0];
    [_sceneBtn1 setTitle:tempDic1[@"name"] forState:UIControlStateNormal];
    
    NSDictionary *tempDic2 = array[1];
    [_sceneBtn2 setTitle:tempDic2[@"name"] forState:UIControlStateNormal];
     
    NSDictionary *tempDic3 = array[2];
    [_sceneBtn3 setTitle:tempDic3[@"name"] forState:UIControlStateNormal];
    
    NSDictionary *tempDic4 = array[3];
    [_sceneBtn4 setTitle:tempDic4[@"name"] forState:UIControlStateNormal];
    
    NSDictionary *tempDic5 = array[4];
    [_sceneBtn5 setTitle:tempDic5[@"name"] forState:UIControlStateNormal];
    
    NSDictionary *tempDic6 = array[5];
    [_sceneBtn6 setTitle:tempDic6[@"name"] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sceneBtnAction:(UIButton *)sender {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SceneSetStoryboard" bundle:nil];
    SceneSetDetailedViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SceneSetDetailedViewController"];
    vc.transitioningDelegate = self;
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    vc.deviceArr = [NSMutableArray arrayWithObject:[[GKBEquipmentsManager shareInstance]getEquipmentsArray]];
    vc.deviceArr = [NSMutableArray arrayWithArray:delegate.rightArray];
    vc.sceneIndex = sender.tag;
    vc.sceneName = sender.titleLabel.text;
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)goBackAction:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma Mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [[PresentTransitionAnimated alloc] init];
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [[DismissTransitionAnimated alloc] init];
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator{
    return _percentDrivenTransition;
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator{
    return _percentDrivenTransition;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc {
    [_sceneBtn1 release];
    [_sceneBtn2 release];
    [_sceneBtn3 release];
    [_sceneBtn4 release];
    [_sceneBtn5 release];
    [_sceneBtn6 release];
    [super dealloc];
}
@end
